package com.google.code.test.stock;

import java.util.ArrayList;
import java.util.List;

public class Stock {

    private List<Product> products;

    public void setProducts(List<Product> products) {

        this.products = products;
    }

    public List<Product> getProducts() {

        if (products == null) {

            products = new ArrayList<Product>();
        }

        return products;
    }

    public void refound(Customer customer, Product product) {

        products.add(product);
        customer.getProducts().remove(product);
    }

    public void replace(Customer customer, Stock stock, Product from, Product to) {

        products.add(from);
        products.remove(to);
        customer.getProducts().add(to);
        customer.getProducts().remove(from);
    }

    public Product getProductByName(String name) {

        for (Product p : products) {

            if (p.getName().equals(name)) {

                return p;
            }
        }

        return null;
    }

    public int countProductsByName(String name) {

        int count = 0;

        for (Product p : products) {

            if (p.getName().equals(name)) {

                count++;
            }
        }

        return count;
    }
}
